﻿@page "/"
@inject OperateServer operateServer
@inject ChangeServer changeServer
@inject ChangeAreaServer changeAreaServer
@inject BusinessDepartmentServer businessDepartmentServer
@inject MessageService _message
@using System.ComponentModel
@using AntDesign.TableModels
<div>
    @*@if (!plcflag)
    {
        <Button Type="@ButtonType.Primary"
                OnClick="@Starpllc">
            连接调度系统
        </Button>

    }
    else
    {
        <Button Danger Type="@ButtonType.Primary" OnClick="@Stoppllc">
            断开调度系统
        </Button>

    }
    @if (!taskflag)
    {
        <Button Type="@ButtonType.Primary"
                OnClick="@Startask">
            启动任务服务
        </Button>
    }
    else
    {
        <Button Danger Type="@ButtonType.Primary" OnClick="@Stoptask">
            停止任务服务
        </Button>
    }
    <Button Danger Type="@ButtonType.Primary" OnClick="@Resh">
        刷新数据
    </Button>*@
</div>

@if (changeEntities.Count() == 0)
{
    <Empty />
 }
else
{
    @foreach (var item in BusinessDepartments)
    {
        <div>
            <h3>@item.Remark</h3>
            <Divider />
            <Row>
                @{var list = ChangeAreasEntitys.Where(x => x.BusinessDepartmentCode == item.Code).ToList();}

                @foreach (var item1 in list)
                {
                    var list2 = new ObservableCollection<ChangeEntity>(changeEntities.Where(x => x.ChangeAreaCode == item1.Code).ToList());
                    <BusinessDataDisplay Changes="@list2" Title="@item1.Remark"></BusinessDataDisplay>
                }
            </Row>
        </div>

    }
}

@*@{var list = ChangeAreasEntitys.Where(x => x.BusinessDepartmentCode == item.Code).ToList();}
    @foreach (var item1 in list)
    {
        var list2 = new ObservableCollection<ChangeEntity>(changeEntities.Where(x => x.ChangeAreaCode == item1.Code).ToList());

    }


    }
    <div>
        <Row>
            <Col Span="24" Lg="8">
            <BusinessDataDisplay Changes="@changeEntities" Title="tttt"></BusinessDataDisplay>
            </Col>
            <Col Span="24" Lg="8">
            <BusinessDataDisplay Changes="@changeEntities" Title="tttt"></BusinessDataDisplay>
            </Col>
            <Col Span="24" Lg="8">
            <BusinessDataDisplay Changes="@changeEntities" Title="tttt"></BusinessDataDisplay>
            </Col>
        </Row>
    </div>*@

@*<div>
        <Carousel Autoplay="TimeSpan.FromSeconds(2)">
            <CarouselSlick>
                <h3>1</h3>
            </CarouselSlick>
            <CarouselSlick>
                <h3>2</h3>
            </CarouselSlick>
            <CarouselSlick>
                <h3>3</h3>
            </CarouselSlick>
            <CarouselSlick>
                <h3>4</h3>
            </CarouselSlick>
        </Carousel>
    </div>*@

@*@foreach (var item in BusinessDepartments)
    {
    <div style=" text-align: center; height: 300px; line-height: 300px; background: #364d79; overflow: hidden;">
        <Carousel style=" text-align: center; height: 300px; line-height: 300px; background: #364d79; overflow: hidden;">
            @{var list = ChangeAreasEntitys.Where(x => x.BusinessDepartmentCode == item.Code).ToList();}
            @foreach (var item1 in list)
            {
                var list2 = new ObservableCollection<ChangeEntity>(changeEntities.Where(x => x.ChangeAreaCode == item1.Code).ToList());
                <CarouselSlick>
                    <BusinessDataDisplay Changes="@list2" Title="@item1.Remark"></BusinessDataDisplay>
                </CarouselSlick>
            }
        </Carousel>
        </div>
      }*@


@*@for (int i = 0; i < BusinessDepartments.Count; i++)
    {
        <div>
            <Carousel>
                @{var list = ChangeAreasEntitys.Where(x => x.BusinessDepartmentCode == BusinessDepartments[i].Code).ToList();}
                @for (int j = 0; j < list.Count(); j++)
                {
                    var list2 = new ObservableCollection<ChangeEntity>(changeEntities.Where(x => x.ChangeAreaCode == list[i].Code).ToList());
                    <CarouselSlick>
                        <BusinessDataDisplay Changes="@list2" Title="@list[i].Remark"></BusinessDataDisplay>
                    </CarouselSlick>
                }
            </Carousel>
        </div>
    }*@


@code {
    string activeKey { get; set; } = "1";
    private ObservableCollection<ChangeEntity> changeEntities = new ObservableCollection<ChangeEntity>();
    private ObservableCollection<ChangeAreasEntity> ChangeAreasEntitys = new ObservableCollection<ChangeAreasEntity>();
    private ObservableCollection<BusinessDepartmentEntitycs> BusinessDepartments = new ObservableCollection<BusinessDepartmentEntitycs>();
    bool plcflag = false;
    bool taskflag = false;
    bool ReshFlag = false;
    void OnTabChange(string key)
    {
        // Console.WriteLine($"tab change:{key}");
    }
    protected override async Task OnInitializedAsync()
    {
        plcflag = await operateServer.GetState(1);
        taskflag = await operateServer.GetState(2);
        var list = await businessDepartmentServer.GetBusinesses();
        if (list != null)
        {
            BusinessDepartments = new ObservableCollection<BusinessDepartmentEntitycs>(list?.ToList());
            if (BusinessDepartments.Count != 0)
            {
                var list1 = await changeAreaServer.GetChangeAreas();
                if (list1 != null)
                {
                    ChangeAreasEntitys = new ObservableCollection<ChangeAreasEntity>(list1);
                    if (ChangeAreasEntitys.Count != 0)
                    {
                        var list2 = await changeServer.GetChanges();
                        if (list2 != null)
                        {
                            changeEntities = new ObservableCollection<ChangeEntity>(list2?.ToList());
                        }

                    }
                }

            }
        }

        await base.OnInitializedAsync();
    }




    private async void Resh()
    {
        if (!ReshFlag)
        {
            ReshFlag = true;
            var list = await businessDepartmentServer.GetBusinesses();
            BusinessDepartments = new ObservableCollection<BusinessDepartmentEntitycs>(list?.ToList());
            if (BusinessDepartments.Count != 0)
            {
                var list1 = await changeAreaServer.GetChangeAreas();
                ChangeAreasEntitys = new ObservableCollection<ChangeAreasEntity>(list1);
                if (ChangeAreasEntitys.Count != 0)
                {
                    var list2 = await changeServer.GetChanges();
                    changeEntities = new ObservableCollection<ChangeEntity>(list2?.ToList());
                }
            }

            await this.InvokeAsync(() => { this.StateHasChanged(); });
            ReshFlag = false;
        }
    }

    private async void Starpllc()
    {
        var result = await operateServer.Operate(1, true);
        await _message.Info($"启动调度--{result}");
        plcflag = await operateServer.GetState(1);
     //   taskflag = await operateServer.GetState(2);
        await this.InvokeAsync(() => { this.StateHasChanged(); });
    }
    private async void Stoppllc()
    {
        var result = await operateServer.Operate(1, false);
        await _message.Info($"停止调度--{result}");
        plcflag = await operateServer.GetState(1);
       // taskflag = await operateServer.GetState(2);
        await this.InvokeAsync(() => { this.StateHasChanged(); });
    }
    private async void Startask()
    {

        var result = await operateServer.Operate(2, true);
        await _message.Info($"启动任务服务--{result}");
       // plcflag = await operateServer.GetState(1);
        taskflag = await operateServer.GetState(2);
        await this.InvokeAsync(() => { this.StateHasChanged(); });
        // this.InvokeAsync(() => { this.StateHasChanged(); });
    }
    private async void Stoptask()
    {
        var result = await operateServer.Operate(2, false);
        await _message.Info($"停止任务服务--{result}");
        //plcflag = await operateServer.GetState(1);
        taskflag = await operateServer.GetState(2);
        await this.InvokeAsync(() => { this.StateHasChanged(); });
    }
}
